//创建路由实例
import {createRouter, createWebHistory} from "vue-router";
import type {RouteRecordRaw} from 'vue-router'
const routes :RouteRecordRaw[]= [
  {
    path: "/login",
    component:()=>import("@/views/login/Login.vue"),
    meta:{title:"登录"}
  },
  {
    path:"/",
    redirect:"/dashboard",
  },
  {
    path: '/document-system',  // 访问路径
    name: 'DocumentSystem',    // 路由名称，用于编程式导航
    component: () => import("@/views/document/DocumentSystem.vue"), // 对应的组件
    meta: {
      title: '律师文件管理',   // 页面标题
      requiresAuth: false      // 可选，标记需要登录才能访问
    }
  },

  {
    path: "/home",
    component:()=>import("@/views/Home.vue"),
    children:[
      {
        path:"/dashboard",
        component:()=>import("@/views/DashBoard.vue"),
        meta:{title:"首页"}
      },
      {path:"/user",
        component:()=>import("@/views/user/User.vue"),
        meta:{title:"用户"}
      },
      {path:"/role",
        component:()=>import("@/views/role/Role.vue"),
        meta:{title:"角色"}
      },
      // 在home的children中添加
      {
        path: "/menu",
        component: () => import("@/views/menu/Menu.vue"),
        meta: { title: "菜单管理", requiresAuth: true }
      },
      {
        path: "/lawyer",
        component: () => import("@/views/lawyer/Lawyer.vue"),
        meta: { title: "律师管理", requiresAuth: true },

      },
      // 新增两个独立路由
      {
        path: "/lawyer/addLawyer",
        name: "AddLawyer", // 新增律师路由名称
        component: () => import("@/views/lawyer/PersonInformation.vue"),
        meta: {
          title: "新增律师信息",
          requiresAuth: true
        }
      },
      {
        path: "/lawyer/editLawyer", // 编辑必须传id参数
        name: "EditLawyer", // 编辑律师路由名称
        component: () => import("@/views/lawyer/PersonInformation.vue"),
        meta: {
          title: "编辑律师信息",
          requiresAuth: true
        }
      },
      {
        path: "/contract1", // 编辑必须传id参数
        name: 'ContractReceive',
        component: () => import("@/views/contract/ContractOne.vue"),
        meta: {
          title: "合同领用",
          requiresAuth: true
        }
      },
      {
        path: '/cancel',
        name: 'ContractCancel',
        component: () => import('@/views/contract/ContractTwo.vue'),
        meta: {
          title: "合同注销",
          requiresAuth: true
        }
      },
      {
        path: '/query',
        name: 'ContractQuery',
        component: () => import('@/views/contract/ContractQuery.vue'),
        meta: {
          title: "合同",
          requiresAuth: true
        }
      },
      // 新增：主任待确认列表作为home子路由
      {
        path: "/document",
        name: 'DirectorApproval',
        component: () => import("@/views/document/DirectorApproval.vue"),
        meta: {
          title: "待确认列表",
          requiresAuth: true,
          role: 'director' // 仅主任可见
        }
      },
      {
        path: "/seal",
        name: 'Seal',
        component: () => import("@/views/seal/Seal.vue"),
        meta: {
          title: "用章管理",
          requiresAuth: true,
        }
      }
    ]
  }
]

//创建路由实例
const router = createRouter({
  history:createWebHistory(),//hash模式
  routes//传递上面定义的路由规则
})

//路由守卫：控制未登录访问
//路由导航守卫,路由访问的拦截
//to：表示要去的地方
//from：表示来源
//next：有两个作用：有参数的表示强制跳转到该位置，没有参数的表示放行
router.beforeEach((to,from,next)=>{

  document.title=`${to.meta.title} | vue-manage-system` // 设置页面标题
  //后台颁发给前台的令牌，获取浏览器存储的token
  //做判断的，读取token，token存在已登录，不存在则未登录
  let token = sessionStorage.getItem("token");


  //（!token：）未登录，（path!=='/login'：）且访问的不是登录页，强制跳转到登录页
  // 检查当前路由是否需要认证
  const requiresAuth = to.meta.requiresAuth !== false; // 默认需要认证
  // 如果需要认证但没有token，跳转到登录页
  if(requiresAuth &&!token&&to.path!=="/login"){
    next("/login")
  }else {
    next()//其他情况放行
  }

})

//导出路由实例
export default router
